All files / src/app/api/admin/monitoring/alerts/[id]/resolve route.ts

0% Statements 0/62
100% Branches 0/0
0% Functions 0/1
0% Lines 0/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                                                                                                                             
export const dynamic = 'force-dynamic';

import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  errorResponse,
  type ApiSuccessResponse,
  type ApiErrorResponse,
  type AuthenticatedUser,
} from '@/lib/api';
import type { PerformanceAlert } from '@prisma/client';
import type { Session } from 'next-auth';

/**
 * POST /api/admin/monitoring/alerts/[id]/resolve
 *
 * Resolve a performance alert.
 * The [id] here refers to the alert ID (not the config ID).
 */
async function handlePost(
  _request: NextRequest,
  context: { params?: Promise<Record<string, string>> } | undefined,
  _session: Session,
  user: AuthenticatedUser
): Promise<NextResponse<ApiSuccessResponse<{ alert: PerformanceAlert }> | ApiErrorResponse>> {
  const params = await context?.params;
  const id = params?.id;

  if (!id) {
    return errorResponse('INVALID_PARAM', 'Alert ID is required', { status: 400 });
  }

  // Check if alert exists
  const existing = await prisma.performanceAlert.findUnique({
    where: { id },
  });

  if (!existing) {
    return errorResponse('NOT_FOUND', 'Alert not found', { status: 404 });
  }

  if (existing.status === 'resolved') {
    return errorResponse('INVALID_STATE', 'Alert is already resolved', { status: 400 });
  }

  const alert = await prisma.performanceAlert.update({
    where: { id },
    data: {
      status: 'resolved',
      resolvedAt: new Date(),
      resolvedBy: user.id,
    },
  });

  return successResponse({ alert });
}

// Export handler with middleware
export const POST = withErrorHandling(withAdmin(handlePost));